package com.gy.hsim.netty.server;


import com.gy.hsim.netty.proto.BserviceProto;
import com.gy.hsim.netty.util.ByteDecoder;
import com.gy.hsim.netty.util.ByteEncoder;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;

/**
 * 服务�? ChannelInitializer
 */
public class NettyServerInitializer extends
		ChannelInitializer<SocketChannel> {

	@Override
    public void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
//		pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
//		pipeline.addLast("decoder", new ProtobufDecoder(BserviceProto.BserviceEntity.getDefaultInstance()));
//	    pipeline.addLast("encoder", new ProtobufEncoder());
		pipeline.addLast("decoder", new ByteArrayDecoder());
	    pipeline.addLast("encoder", new ByteArrayEncoder());
        pipeline.addLast("handler", new NettyServerHandler());
 
		System.out.println("Client:"+ch.remoteAddress() +"连接");
		BserviceProto.BserviceEntity.Builder entity = BserviceProto.BserviceEntity.newBuilder();
		entity.setToUid("to:uid" );
		ch.writeAndFlush(entity);
    }
}
